package light.mvc.listener;

import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import light.mvc.service.sys.ResourceServiceI;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class InitSystemListener implements ServletContextListener {

	private static final Logger logger = Logger.getLogger(InitSystemListener.class);
	private Log log = LogFactory.getLog(this.getClass());
	public void contextInitialized(ServletContextEvent sce) {
		// ApplicationContext ac = sce.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
		ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
		ResourceServiceI resourceService = (ResourceServiceI) ac.getBean("resourceServiceImpl");
		// 准备权限数据，只是在程序初始化的时候只准备一次（放到application作用域）
		List<String> resourceList = resourceService.resourceAllList();
		sce.getServletContext().setAttribute("resourceList", resourceList);
		logger.info("------------- 程序初始化完毕 -------------");
		log.info("------------- 程序初始化完毕 -------------");
	}

	public void contextDestroyed(ServletContextEvent sce) {
	}

}
